// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SongChooser.java

package presenter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.plaf.basic.BasicInternalFrameUI;

// Referenced classes of package presenter:
//            SongFile

public class SongChooser extends JPanel implements MouseListener, KeyListener {
    
    JList songList;
    JScrollPane scrollPane;
    SongFile songs[];
    boolean songToAdd;
    private SongFile songAdding;
    private boolean update;
    private JTabbedPane tabs;
    private Videos videos;
    private BibleVerses bv;
    
    public SongChooser()
    {
        super(new BorderLayout());
    }

    public SongChooser(File f, Bible bible)
    {
        this();
        tabs=new JTabbedPane(JTabbedPane.TOP);
        if(f.isDirectory())
        {
            File s[] = f.listFiles();
            File songs[] = new File[s.length];
            int index = 0;
            for(int i = 0; i < s.length; i++)
                if(!s[i].isDirectory()&&!s[i].isHidden())
                {
                    songs[index] = s[i];
                    index++;
                }

            this.songs = new SongFile[index];
            for(int i = 0; i < songs.length; i++){
                if(songs[i]!=null)
                this.songs[i] = new SongFile(songs[i]);
            }
            Arrays.sort(this.songs);
            songList = new JList(this.songs);
            songList.setSelectedIndex(0);
            scrollPane = new JScrollPane();
            scrollPane.getViewport().setView(songList);
            scrollPane.setVisible(true);
            tabs.add("Songs",scrollPane);
            bv=new BibleVerses();
            tabs.add("Bible Verses",bv);
            videos=new Videos(new File("Videos"));
            tabs.add("Videos",videos);
            add(tabs,BorderLayout.CENTER);
            songList.addMouseListener(this);
            songList.addKeyListener(this);
            songToAdd = false;
        }
        
       
    }
    
    public BibleVerses getBibleChooser() {
        return bv;
    }

    public boolean isSongToAdd()
    {
        return songToAdd;
    }

    public SongFile getSongAdding()
    {
        songToAdd = false;
        return songAdding;
    }

    public SongFile[] getSongs()
    {
        return songs;
    }

    public boolean isUpdated()
    {
        if(update||bv.isUpdated()||videos.isUpdated())
        {
            //System.out.println("updated");
            update = false;
            return true;
        } else
        {
            return false;
        }
    }

    public SongFile getSelected()
    {
        if(tabs.getSelectedIndex()==0)
            return songs[songList.getSelectedIndex()];
        if(tabs.getSelectedIndex()==1)
            return bv.getSong();
                    
        return videos.getSong();
    }

    public void mouseClicked(MouseEvent mouseevent)
    {
    }

    public void mousePressed(MouseEvent me)
    {
        songList.requestFocusInWindow();
        update = true;
        if(me.getClickCount() == 2)
        {
            int index = songList.locationToIndex(me.getPoint());
            songToAdd = true;
            songAdding = songs[index];
        }
    }

    public void mouseReleased(MouseEvent mouseevent)
    {
    }

    public void mouseEntered(MouseEvent mouseevent)
    {
    }

    public void mouseExited(MouseEvent mouseevent)
    {
    }
    
    public boolean requestFocusInWindow() {
        boolean test=songList.requestFocusInWindow();
        return test;
    }
    
    public void addKeyListener(KeyListener listener) {
        songList.addKeyListener(listener);
    }

    

    public void setUpdated(boolean updated) {
        update=updated;
    }

    public void keyTyped(KeyEvent e) {
        
    }

    public void keyPressed(KeyEvent e) {
        
    }

    public void keyReleased(KeyEvent e) {
        update=true;
        int x=e.getModifiers();
        int key=e.getKeyCode();
        if(x==0 &&key==10) {
            songToAdd = true;
            songAdding=(SongFile)songList.getSelectedValue();
        }
    }
    
    public boolean exists(long songNumber) {
        for(int i=0; i<songs.length; i++) {
            if(songs[i].getNumber()==songNumber)
                return true;
        }
        return false;
    }
    
    public int getNumber() {
        for(int i=1; i<777777; i++) {
            if(!exists(i))
                return i;
        }
        return -200;
    }
    
    public void addSong(SongFile songAdding) {
        ArrayList<SongFile> songList =new ArrayList<SongFile>();
        for(int i=0; i<songs.length; i++)
            songList.add(songs[i]);
        songList.add(songAdding);
        songs=songList.toArray(new SongFile[1]);
        Arrays.sort(songs);
        this.songList.setListData(songs);
    }
}
